package com.szbber.medical.board.viewModel

import android.content.Context
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.utils.ToastUtils
import com.szbber.commen.api.Repository
import com.szbber.commen.api.launch
import com.szbber.commen.base.BaseViewModel
import com.szbber.commen.bean.*
import com.szbber.commen.dialog.DialogUtils
import com.szbber.commen.utils.dataStoreRead

class PatientDetailsFragmentViewModel : BaseViewModel() {

    val patientDetailsBean = MutableLiveData<PatientDetailsBean>()
    fun getPatientInfo(context: Context?, cureNo: String?) = launch(context, {
        val queue = arrayOf("patientId", cureNo)
        patientDetailsBean.value = Repository.getPatientInfo(context, *queue)
    })

    val nurseLevelBean = MutableLiveData<NurseLevelListBean>()
    fun getNurseLevel(context: Context?, cureNo: String?) = launch(context, {
        val data = Repository.getNurseLevel(context)
        nurseLevelBean.value = data
        DialogUtils.showNurseLevelDialog(context, data) {
            launch {
                val queue = arrayOf("nurseLevel", it.nurseLevel?.level_code, "cureNo", cureNo)
                Repository.editPatientInfo(context, *queue)
                getPatientInfo(context, cureNo)
            }
        }
    })

    val deptBean = MutableLiveData<DeptBean>()
    fun getDeptList(context: Context?, cureNo: String?) = launch(context, {
        val data = Repository.getDeptBean(context)
        deptBean.value = data
        DialogUtils.showDeptBeanDialog(context, data) {
            launch {
                val queue = arrayOf("deptCode", it.deptBean?.code, "cureNo", cureNo)
                Repository.editPatientInfo(context, *queue)
                getPatientInfo(context, cureNo)
            }
        }
    })
    
    val doctorListBean = MutableLiveData<DoctorListBean>()
    fun getDoctorList(context: Context?, cureNo: String?) = launch(context, {
        val data = Repository.getDoctorList(context)
        doctorListBean.value = data
        DialogUtils.showDoctorListDialog(context, data) {
            launch {
                val queue = arrayOf("doctorCode", it.doctorListBean?.code, "cureNo", cureNo)
                Repository.editPatientInfo(context, *queue)
                getPatientInfo(context, cureNo)
            }
        }
    })

    val nurseListBean = MutableLiveData<NurseListBean>()
    fun getNurseList(context: Context?, cureNo: String?) = launch(context, {
        val data = Repository.getNurseList(context)
        nurseListBean.value = data
        DialogUtils.showNurseListDialog(context, data) {
            launch {
                val queue = arrayOf("nurseCode", it.nurseListBean?.code, "cureNo", cureNo)
                Repository.editPatientInfo(context, *queue)
                getPatientInfo(context, cureNo)
            }
        }
    })

    fun etInTimer(context: Context?, cureNo: String?) = launch(context, {
        DialogUtils.showCheckTimer(context!!) {
            launch {
                val queue = arrayOf("inTimer", it.strValue, "cureNo", cureNo)
                Repository.editPatientInfo(context, *queue)
                getPatientInfo(context, cureNo)
            }
        }
    })

    fun getCareLevelList(context: Context?, cureNo: String?)=launch(context,{
        val data = Repository.getCareLevelList(context)
        DialogUtils.showCareListBean(context, data){
            launch {
                addCareLevel(context,cureNo,  it.careLevelListBean?.label_code)
            }
        }
    })

    fun addCareLevel(context: Context?,cureNo: String?, careLabelId:String?) = launch(context,{
       val deptCode =  dataStoreRead<String>("deptCode")
        val bedCode = dataStoreRead<String>("bedCode")
        val queue = arrayOf("patientId", cureNo, "careLabelId",careLabelId,"bedCode", bedCode,"deptCode", deptCode)
            Repository.addCareLevel(context, *queue)
            getPatientInfo(context, cureNo)

    })

}